﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for DA_PaymentModeLevel
/// </summary>
public class DA_PaymentLevel
{
    AuditBillingEntities objABEnt;
    public DA_PaymentLevel()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public List<PLPaymentLevel> DA_fnGetPaymentModeLevelList(PLPaymentLevel pObjPaymentModeLevel)
    {
        using (objABEnt = new AuditBillingEntities())
        {
            var varRes = (from u in objABEnt.MstPaymentLevels
                          join pm in objABEnt.MstPaymentModes on u.MstPaymentModeID equals pm.MstPaymentModeID
                          where (pObjPaymentModeLevel.SearchIsActive == null || u.IsActive == pObjPaymentModeLevel.SearchIsActive) &&
                                (pObjPaymentModeLevel.PaymentLevelID == null || pObjPaymentModeLevel.PaymentLevelID == u.MstPaymentModeID) &&
                                (string.IsNullOrEmpty(pObjPaymentModeLevel.Name) || u.Name.ToLower().Contains(pObjPaymentModeLevel.Name.ToLower()))
                          orderby pm.PaymentModeName, u.LevelSort
                          select new PLPaymentLevel()
                          {
                              PaymentLevelID = u.MstPaymentLevelID,
                              PaymentModeID = u.MstPaymentModeID,
                              PaymentModeName = pm.PaymentModeName,
                              Name = u.Name,
                              Abbriviation = u.Abbriviation,
                              Percentage = u.Percentage,
                              PropDurationInMonth = u.PropDurationInMonth,
                              LevelSort = u.LevelSort,
                              IsActive = u.IsActive
                          });
            return varRes.ToList();

        }
    }

    public int DA_InsertUpdatePaymentLevel(PLPaymentLevel pObjPaymentLevel)
    {
        using (objABEnt = new AuditBillingEntities())
        {
            MstPaymentLevel objPaymentLevel = new MstPaymentLevel();
            if (pObjPaymentLevel.PaymentLevelID.GetValueOrDefault() != 0)
            {
                objPaymentLevel = (from u in objABEnt.MstPaymentLevels
                                  where u.MstPaymentLevelID == pObjPaymentLevel.PaymentLevelID
                                  select u).ToList().FirstOrDefault();
            }
            objPaymentLevel.MstPaymentModeID = pObjPaymentLevel.PaymentModeID.GetValueOrDefault();
            objPaymentLevel.Name = pObjPaymentLevel.Name;
            objPaymentLevel.Abbriviation = pObjPaymentLevel.Abbriviation;
            objPaymentLevel.Percentage = pObjPaymentLevel.Percentage;
            objPaymentLevel.PropDurationInMonth = pObjPaymentLevel.PropDurationInMonth;
            objPaymentLevel.LevelSort = pObjPaymentLevel.LevelSort;

            objPaymentLevel.IsActive = pObjPaymentLevel.IsActive;

            if (pObjPaymentLevel.PaymentLevelID.GetValueOrDefault() != 0)
            {
                pObjPaymentLevel.ModifiedBy = SessionInfo.prop_getObj.EmployeeID;
                pObjPaymentLevel.ModifiedOn = DateTime.Now;
            }
            else
            {
                pObjPaymentLevel.CreatedBy = SessionInfo.prop_getObj.EmployeeID;
                pObjPaymentLevel.CreatedOn = DateTime.Now;
                objABEnt.MstPaymentLevels.Add(objPaymentLevel);
            }
            objABEnt.SaveChanges();
            return objPaymentLevel.MstPaymentLevelID;
        }
    } 
}